package com.ssy.lingxi.message.serviceimpl;

import com.ssy.lingxi.common.constant.basic.UserLoginSourceEnum;
import com.ssy.lingxi.message.api.constant.WsAction;
import com.ssy.lingxi.message.service.IMemberMessageService;
import com.ssy.lingxi.message.websocket.WsMessagePushService;
import com.ssy.lingxi.message.websocket.model.WsMessage;
import com.ssy.lingxi.message.websocket.session.WsSessionKey;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 消息事件服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2020/10/27
 */
@Service
public class MessageEventService {

    @Resource
    private IMemberMessageService memberMessageService;
    @Resource
    private WsMessagePushService wsMessagePushService;

    public void onMessage(Long memberId, Long roleId, Long userId) {
        // 推送未读消息数
        WsMessage wsMessage = WsMessage.builder().action(WsAction.msg_unread_message)
                .receiver(WsSessionKey.buildByMemberIdAndRoleIdAndUserId(memberId, roleId, userId))
                .data(memberMessageService.countUnreadMemberMessage(memberId, roleId, userId) + "")
                .build();

        wsMessagePushService.pushMessage(wsMessage);
    }

    public void onPlatformMessage() {
        // 推送未读消息数
        WsMessage wsMessage = WsMessage.builder().action(WsAction.msg_unread_message)
                .receiver(WsSessionKey.buildBySource(UserLoginSourceEnum.BUSINESS_MANAGEMENT_PLATFORM.getCode()))
                .data(memberMessageService.countUnreadPlatformMessage() + "")
                .build();

        wsMessagePushService.pushMessage(wsMessage);
    }
}
